﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Generator.Selections.DataDictionary;
using Generator.Selections.Repository;

namespace Generator.Selections.Windows
{
    public partial class TableSelectionForm : Form
    {
        public TableSelectionForm()
        {
            InitializeComponent();
        }

        public string NameSpace
        {
            get { return nameSpaceTextBox.Text; }
            set { nameSpaceTextBox.Text= value; }
        }

        public List<Table> SelectedTables
        {
            get 
            { 
                var result = new List<Table>();

                foreach (var selectedTable in _selectedTableList)
                {
                    if (selectedTable.Selected)
                    {
                        result.Add(selectedTable);
                    }
                }
                return result;
            } 
        }
        private List<SelectedTable> _selectedTableList = new List<SelectedTable>();
        private List<string> _owners = new List<string>();

        public void Init()
        {
            _owners = new OwnerRepository().GetUserNames();
            ownerBindingSource.DataSource = _owners;
            ownerComboBox.SelectedIndex = -1;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            DialogResult= DialogResult.OK;
        }

        private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var editor = sender as ComboBox;
            if (editor == null) return;

            string owner = null;
            if (editor.SelectedItem != null)
                owner = editor.SelectedItem.ToString();

            if (owner == null) return;

            _selectedTableList = new TableRepository().GetTablesByOwner(owner);

            tableBindingSource.DataSource = _selectedTableList;
        }

        private void tableNameLiketextBox_Validated(object sender, EventArgs e)
        {
            var editor = sender as TextBox;
            if (editor == null) return;

            string tableName = null;
            if (editor.Text != null)
                tableName = editor.Text;

            if (tableName == null) return;

            Cursor = Cursors.WaitCursor;
            try
            {
                var list = new TableRepository().GetTablesByNameLike(tableName);
                MergeToSelectedTableList(list);
                tableBindingSource.DataSource = _selectedTableList;
                tableBindingSource.ResetBindings(false);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void MergeToSelectedTableList(List<SelectedTable> list)
        {
            foreach (var selectedTable in list)
            {
                if (_selectedTableList.Find(t => t.TableName == selectedTable.TableName) == null)
                    _selectedTableList.Add(selectedTable);
            }
        }


        private void tablesDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            var dataGridView = sender as DataGridView;
            if (dataGridView == null || dataGridView.CurrentCell == null) return;
            
            if (dataGridView.CurrentCell.ColumnIndex == 2 && dataGridView.IsCurrentCellDirty)
            {
                dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void tablesDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var dataGridView = sender as DataGridView;
            if (dataGridView == null || dataGridView.Rows.Count == 0) return;

            if (dataGridView.Columns[e.ColumnIndex] == tablesDataGridViewSelected)
            {
                var selectedValue = dataGridView.Rows[e.RowIndex].Cells[tablesDataGridViewSelected.Name].Value;
                if (selectedValue == null) return;

                var selected = (bool)selectedValue;

                if (selected)
                {
                    Cursor = Cursors.WaitCursor;
                    try
                    {
                        var tableName = dataGridView.Rows[e.RowIndex].Cells[tablesDataGridViewTableName.Name].Value.ToString();
                        var table = _selectedTableList.Find(t => t.TableName == tableName);
                        if (table.ColumnList == null || table.ColumnList.Count == 0)
                        {
                            table.ColumnList = new ColumnRepository().GetColumns(tableName);
                        }
                        tableBindingSource.ResetBindings(false);
                    }
                    finally
                    {
                        Cursor = Cursors.Default;
                    }
                }
            }
        }

        private void tableNameLikeTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            SelectNextControl(ActiveControl, true, true, true, true);
            e.Handled = true;
        }
    }
}
